package com.jx.medical.controller;

import com.jx.medical.pojo.*;
import com.jx.medical.service.QueryService;
import com.jx.medical.vo.MedicineTable;
import com.jx.medical.vo.ShowInRecord;
import com.jx.medical.vo.ShowOutRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author jx
 * @date Created on 2020/12/4  10:29
 **/
@RestController
@CrossOrigin
@RequestMapping("/query")
public class QueryController {

    @Autowired
    private QueryService queryService;

    @RequestMapping("/queryMedicine")
    public AjaxInfo queryMedicine(HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            List<Medicine> medicines = queryService.queryMedicine();
            ajaxInfo.setCode(0);
            ajaxInfo.setMsg("查询成功");
            ajaxInfo.setData(medicines);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/queryFactory")
    public AjaxInfo queryFactory(HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            List<Factory> factories = queryService.queryFactory();
            ajaxInfo.setCode(0);
            ajaxInfo.setData(factories);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/queryBuyer")
    public AjaxInfo queryBuyer1(HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            List<Buyer> buyers = queryService.queryBuyer();
            ajaxInfo.setCode(0);
            ajaxInfo.setData(buyers);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/showInRecord")
    public AjaxInfo showInRecord(MedicineTable medicineTable,HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            int page=(medicineTable.getPage()-1)*medicineTable.getLimit();
            medicineTable.setPage(page);
            List<ShowInRecord> inRecord = queryService.getInRecord(medicineTable);
            ajaxInfo.setCount(queryService.getInRecordCount(medicineTable));
            ajaxInfo.setCode(0);
            ajaxInfo.setData(inRecord);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

    @RequestMapping("/showOutRecord")
    public AjaxInfo queryBuyer(MedicineTable medicineTable,HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        if (session.getAttribute("userName") != null) {
            int page=(medicineTable.getPage()-1)*medicineTable.getLimit();
            medicineTable.setPage(page);
            List<ShowOutRecord> outRecord = queryService.getOutRecord(medicineTable);
            ajaxInfo.setCount(queryService.getOutRecordCount(medicineTable));
            ajaxInfo.setCode(0);
            ajaxInfo.setData(outRecord);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }
    @RequestMapping("/queryMedicineByName")
    public AjaxInfo queryMedicineByName(MedicineTable medicineTable, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        System.out.println(medicineTable);
        if (session.getAttribute("userName") != null) {
            int page=(medicineTable.getPage()-1)*medicineTable.getLimit();
            medicineTable.setPage(page);
            List<Medicine> medicines = queryService.queryMedicineByName(medicineTable);
            ajaxInfo.setMsg("查询成功");
            ajaxInfo.setCount(queryService.queryMedicineByNameCount(medicineTable));
            ajaxInfo.setCode(0);
            ajaxInfo.setData(medicines);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }
    @RequestMapping("/queryFactoryByName")
    public AjaxInfo queryFactoryByName(MedicineTable medicineTable, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        System.out.println(medicineTable);
        if (session.getAttribute("userName") != null) {
            int page=(medicineTable.getPage()-1)*medicineTable.getLimit();
            medicineTable.setPage(page);
            List<Factory> factories = queryService.queryFactoryByName(medicineTable);
            ajaxInfo.setMsg("查询成功");
            ajaxInfo.setCount(queryService.queryFactoryByNameCount(medicineTable));
            ajaxInfo.setCode(0);
            ajaxInfo.setData(factories);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }
    @RequestMapping("/queryBuyerByName")
    public AjaxInfo queryBuyerByName(MedicineTable medicineTable, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        System.out.println(medicineTable);
        if (session.getAttribute("userName") != null) {
            int page=(medicineTable.getPage()-1)*medicineTable.getLimit();
            medicineTable.setPage(page);
            List<Buyer> buyers = queryService.queryBuyerByName(medicineTable);
            ajaxInfo.setMsg("查询成功");
            ajaxInfo.setCount(queryService.queryBuyerByNameCount(medicineTable));
            ajaxInfo.setCode(0);
            ajaxInfo.setData(buyers);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }
    @RequestMapping("/queryUserByName")
    public AjaxInfo queryUserByName(MedicineTable medicineTable, HttpSession session) {
        AjaxInfo ajaxInfo = new AjaxInfo();
        System.out.println(medicineTable);
        if (session.getAttribute("userName") != null) {
            int page=(medicineTable.getPage()-1)*medicineTable.getLimit();
            medicineTable.setPage(page);
            List<User> users = queryService.queryUserByName(medicineTable);
            ajaxInfo.setMsg("查询成功");
            ajaxInfo.setCount(queryService.queryUserByNameCount(medicineTable));
            ajaxInfo.setCode(0);
            ajaxInfo.setData(users);
        } else {
            ajaxInfo.setCode(-2);
            ajaxInfo.setMsg("权限不足！请先登录~");
        }
        return ajaxInfo;
    }

}
